package controller;

import java.util.LinkedList;
import java.util.List;

import javax.el.ELContext;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

import data.GoTData;

import model.House;
import model.HouseList;
import model.InputHasSpouse;
import model.InputHasTitle;
import model.InputPerson;
import model.Person;

public class GoTController {
	
	GoTData gotData = null;
	
	public GoTController() {
		gotData = GoTData.Instance();
	}
	
	public String queryHouseInformation() {
		
		HouseList houseList = null;
		House house = null;
		
		ELContext ctx = FacesContext.getCurrentInstance().getELContext();
		
		houseList = (HouseList) ctx.getELResolver().getValue(ctx, null,
				"houseList");
				
		house = (House) ctx.getELResolver().getValue(ctx, null,
				"house");
		
		house = houseList.getSelectedHouse();
		
		house.setMembers(gotData.getMembers(house.getName()));
		
		
		System.out.println(houseList.getSelectedHouse().getName());


		return "showHouse";
	}

	public void queryPersonInformation(Person selected) {
		System.out.println("queryPersonInformation: "+selected.getName());
		
		HouseList houseList = null;
		House house = null;
		Person person = null;
		
		System.out.println("1.");
		
		ELContext ctx = FacesContext.getCurrentInstance().getELContext();

		houseList = (HouseList) ctx.getELResolver().getValue(ctx, null,
				"houseList");
		
		System.out.println("2.");
		
		//house = (House) ctx.getELResolver().getValue(ctx, null,
		//		"house");
		
		//person = (Person) ctx.getELResolver().getValue(ctx, null,
		//		"person");
		
		person = selected;
		
		house = houseList.getSelectedHouse();
		
		//person = house.getSelectedPerson();
		
		System.out.println("3.Person name: "+person.getName());
		
		OWLNamedIndividual indiv_person = gotData.getIndividual(person.getName());
		person.setSiblings(gotData.getSiblings(indiv_person));
		person.setHeirOf(gotData.getHeirOf(indiv_person));
		person.setTitles(gotData.getTitles(indiv_person));
		person.setAlive(gotData.isAlive(indiv_person));
		person.setYearOfBirth(gotData.getYearOfBirth(indiv_person));
		person.setChildren(gotData.getChildren(indiv_person));
		person.setInheritanceStatus(gotData.getInheritanceStatus(indiv_person));
		
		person.setFather(gotData.getFather(indiv_person));
		person.setMother(gotData.getMother(indiv_person));
		person.setSpouse(gotData.getSpouse(indiv_person));
		
		house.setSelectedPerson(person);
		
		System.out.println("Name: "+person.getName());
		System.out.println("Age: "+person.getAge());

		RequestContext rctx = RequestContext.getCurrentInstance();
		rctx.update("gotDataForm:outputPersonData");
	}
	
	public void addPerson() {
		System.out.println("i add the person");
		
		ELContext ctx = FacesContext.getCurrentInstance().getELContext();
		
		//InputPerson inputPerson = (InputPerson)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("inputPerson");
		
		InputPerson inputPerson = (InputPerson) ctx.getELResolver().getValue(ctx, null,
				"inputPerson");
		
		System.out.println("inputPerson: "+inputPerson.getName()+", "+inputPerson.getAge());
		
		gotData.addPerson(inputPerson.getName(), inputPerson.getAge(), inputPerson.getSex(), inputPerson.getFatherName(), inputPerson.getMotherName());
		
		RequestContext rctx = RequestContext.getCurrentInstance();
		
		HouseList houseList = (HouseList) ctx.getELResolver().getValue(ctx, null,
				"houseList");
		
		houseList.getSelectedHouse().setMembers(gotData.getMembers(houseList.getSelectedHouse().getName()));
		
		//System.out.println("house: "+house.getName());
		//System.out.println(houseList.getSelectedHouse().getMembers());
		
		rctx.update("gotDataForm:membersList");
		
		rctx.execute("dlg1.hide()");
	}

	public void assignTitle() {
		ELContext ctx = FacesContext.getCurrentInstance().getELContext();
		
		InputHasTitle inputHasTitle = (InputHasTitle) ctx.getELResolver().getValue(ctx, null, "inputHasTitle");
		
		HouseList hl = (HouseList) ctx.getELResolver().getValue(ctx, null, "houseList");
		
		Person p = hl.getSelectedHouse().getSelectedPerson();
		
		gotData.addTitle(p.getName(), inputHasTitle.getTitle());
		
		RequestContext rctx = RequestContext.getCurrentInstance();
		
		hl.getSelectedHouse().setSelectedPerson(p);
		queryPersonInformation(p);
		
		rctx.execute("dlgHasTitle.hide()");
	}
	
	public void assignSpouse() {
		ELContext ctx = FacesContext.getCurrentInstance().getELContext();
		
		InputHasSpouse inputHasSpouse = (InputHasSpouse) ctx.getELResolver().getValue(ctx, null, "inputHasSpouse");
		
		HouseList hl = (HouseList) ctx.getELResolver().getValue(ctx, null, "houseList");
		
		Person p = hl.getSelectedHouse().getSelectedPerson();
		
		gotData.marry(p.getName(), inputHasSpouse.getPersonTwo());
		
		RequestContext rctx = RequestContext.getCurrentInstance();
		
		hl.getSelectedHouse().setMembers(gotData.getMembers(hl.getSelectedHouse().getName()));
		hl.getSelectedHouse().setSelectedPerson(p);
		
		queryPersonInformation(p);
		rctx.update("gotDataForm:membersList");
		
		rctx.execute("dlgHasSpouse.hide()");
	}
	
	
	public void switchPersonStatus() {
		System.out.println("-- switch person status");
		
		ELContext ctx = FacesContext.getCurrentInstance().getELContext();
		HouseList hl = (HouseList) ctx.getELResolver().getValue(ctx, null, "houseList");
		
		Person p = hl.getSelectedHouse().getSelectedPerson();
		
		if (p != null) {
			gotData.switchPersonStatus(p.getName());
		} else  {
			System.out.println("-- no person selected");	
		}
		
		RequestContext rctx = RequestContext.getCurrentInstance();
		
		HouseList houseList = (HouseList) ctx.getELResolver().getValue(ctx, null,
				"houseList");
		
		houseList.getSelectedHouse().setMembers(gotData.getMembers(houseList.getSelectedHouse().getName()));
		houseList.getSelectedHouse().setSelectedPerson(p);
		
		queryPersonInformation(p);
		rctx.update("gotDataForm:membersList");
	}


}
